package controllers;

import helpers.UrlHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Author;
import entities.AuthorQuestion;
import entities.Category;
import entities.Question;

import models.AuthorModel;
import models.AuthorQuestionModel;
import models.CategoryModel;
import models.QuestionModel;

public class AuthorServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String params = path.replaceAll(request.getServletPath() + "/", "");	
		
		String id = UrlHelper.getIdFromInfoUrl(params);
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/");
			return ;
		}
		
		AuthorModel authorModel = AuthorModel.getInstance();
		Author author = authorModel.getAuthor(id);
		
		if (author == null) {
			response.sendRedirect("/");
			return ;
		}
		
		//Get Author Question
		AuthorQuestionModel aqModel = AuthorQuestionModel.getInstance();
		AuthorQuestion authorQuestion = aqModel.getAuthorQuestion(author.getId());
		
		QuestionModel questionModel = QuestionModel.getInstance();
		List<Question> questions = new ArrayList<Question>();
		List<Question> answers = new ArrayList<Question>();
		
		if (authorQuestion != null) {
			questions = questionModel.getByListIds(authorQuestion.getqIds());
			answers = questionModel.getByListIds(authorQuestion.getaIds());
		}
		
		List<String> aIds = new ArrayList<String>();
		List<String> qIds = new ArrayList<String>();
		List<String> caIds = new ArrayList<String>();
		List<String> cqIds = new ArrayList<String>();
	
		for (int i = 0; i < questions.size(); i++) {
			qIds.add(questions.get(i).getAuthor());
			cqIds.add(questions.get(i).getCategory());
		}
		
		for (int i = 0; i < answers.size(); i++) {
			aIds.add(answers.get(i).getAuthor());
			caIds.add(answers.get(i).getCategory());
		}
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		
		Map<String, Author> qAuthors = authorModel.getByListIds(qIds);
		Map<String, Author> aAuthors = authorModel.getByListIds(aIds);
		Map<String, Category> qCategories = categoryModel.getByListIds(cqIds);
		Map<String, Category> aCategories = categoryModel.getByListIds(caIds);
		
		/*//Cache header
		response.addHeader("Cache-Control", "public");
		response.addHeader("Cache-Control", "max-age=" + 60000);*/
		
		request.setAttribute("author", author);
		request.setAttribute("authorQuestion", authorQuestion);
		request.setAttribute("questions", questions);
		request.setAttribute("answers", answers);
		request.setAttribute("qAuthors", qAuthors);
		request.setAttribute("aAuthors", aAuthors);
		request.setAttribute("qCategories", qCategories);
		request.setAttribute("aCategories", aCategories);
		try {
			request.getRequestDispatcher("/pages/author.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
